const express = require("express");

const app = express();

const dir = express.static("./public");
app.use(dir);

// http://127.0.0.1:9527/users

/*
HTTP/1.1 200 OK
*/
app.get("/status", (req, res) => {
  // res.status(201); //设置状态码
  // res.status(404);
  // res.status(500);
  // res.send("");
  res.sendStatus(404);
});

app.get("/img", (req, res) => {
  if (new Date().getSeconds() % 2 === 0) {
    res.sendFile("F:/code-209/server/public/img/1.jpg");
  } else {
    res.sendFile("F:/code-209/server/public/img/2.jpg");
  }
});

app.get("/css", (req, res) => {
  res.send(`h1{color:red;}`);
});

app.get("/js", (req, res) => {
  res.send(`console.log("hello js")`);
});

app.get("/xml", (req, res) => {
  // res.header("Content-Type", "application/xml");
  res.type("xml");
  res.send(`
  <user>
    <age>20</age>
    <name>jack</name>
    <gender>boy</gender>
  </user>`);
});

app.get("/json", (req, res) => {
  /*
    自动将对象转换成JSON字符串
  */
  // {"name":"jack","age":20,"gender":"boy"}
  res.send({ name: "jack", age: 20, gender: "boy" });
});

app.get("/users", (req, res) => {
  res.send("<h1>hello jack</h1>");
});

app.listen(9527, () => {
  console.log("success: http://127.0.0.1:9527");
});
